package com.tlias.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tlias.pojo.Emp;
import lombok.SneakyThrows;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class CommomUtils {
    /**
     * String -> LocalDate
     *
     * @param dateStr
     * @return
     */
    public static LocalDate parseDate(String dateStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(dateStr, dateTimeFormatter);
        return date;
    }

    /**
     * LocalDateTime -> String
     *
     * @param dateTime
     * @return
     */
    public static String toStringDateTime(LocalDateTime dateTime) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dateTimeFormatter.format(dateTime);
    }

    /**
     * pojo -> map
     *
     * @param pojo
     * @return
     */
   public static Map<String, Object> pojoToMap(Object pojo) {
        Map<String, Object> map = null;
        try {
            map = new HashMap<>();
            JSONObject jsonObject = (JSONObject) JSON.toJSON(pojo);
            map = JSON.<Map<String, Object>>parseObject(jsonObject.toJSONString(), Map.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 校验日期字符串格式
     * @param dateStr
     * @return
     */
    public static Boolean matchDate(String dateStr){
        String timeRegex1 = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|"+
                "((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|"+
                "((0[48]|[2468][048]|[3579][26])00))-02-29)$";
        return Pattern.matches(timeRegex1, dateStr);
    }

    /**
     * 校验时间字符串格式
     * @param dateTimeStr
     * @return
     */
    public static Boolean matchDateTime(String dateTimeStr){
        String timeRegex = "^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";

        return Pattern.matches(timeRegex, dateTimeStr);
    }

//
//        @SneakyThrows
//        public  static boolean hasNullOrEmptyField(Object obj){
//            Class clazz = obj.getClass();
//            Field[] fields = clazz.getDeclaredFields();
//            for (Field f :
//                    fields) {
//                f.setAccessible(true);
//                Object value = f.get(obj);
//                if(null == value || value.equals("")){
//                    return true;
//                }
//            }
//            return false;
//        }

}
